﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BarberShop.BLL;
using BarberShop.Data;
using Maticsoft.Common;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.IO;
using BarberShop.BLL;
using BarberShop.Common.Enum;


public partial class ProductAdd : BasePage
{
    public int Id
    {
        get
        {
            return string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : Convert.ToInt32(Request.QueryString["id"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetStores();
            GetData();
            GetProductType();
        }
    }


    //#region 上传图片
    ///// <summary>
    ///// asp.net上传图片并生成缩略图
    ///// </summary>
    ///// <param name="upImage">HtmlInputFile控件</param>
    ///// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>
    ///// <param name="sThumbExtension">缩略图的thumb</param>
    ///// <param name="intThumbWidth">生成缩略图的宽度</param>
    ///// <param name="intThumbHeight">生成缩略图的高度</param>
    ///// <returns>缩略图名称</returns>
    //public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight, out string sFilename, out string sThumbFile)
    //{
    //    //string sThumbFile = "";
    //    //string sFilename = "";
    //    sThumbFile = "";
    //    sFilename = "";
    //    if (upImage.PostedFile != null)
    //    {
    //        HttpPostedFile myFile = upImage.PostedFile;
    //        int nFileLen = myFile.ContentLength;
    //        if (nFileLen == 0)
    //            return "没有选择上传图片";
    //        //获取upImage选择文件的扩展名
    //        string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
    //        //判断是否为图片格式
    //        if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
    //            return "图片格式不正确";

    //        byte[] myData = new Byte[nFileLen];
    //        myFile.InputStream.Read(myData, 0, nFileLen);
    //        //sFilename = System.IO.Path.GetFileName(myFile.FileName);
    //        //int file_append = 0;
    //        ////检查当前文件夹下是否有同名图片,有则在文件名+1
    //        //while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
    //        //{
    //        //    file_append++;
    //        //    sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
    //        //        + file_append.ToString() + extendName;
    //        //}
    //        sFilename = UploadUtil.mikecat_GetNumberRandom() + extendName;
    //        //没则创建
    //        if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath)))
    //        {
    //            Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(sSavePath));
    //        }
    //        System.IO.FileStream newFile
    //            = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
    //            System.IO.FileMode.Create, System.IO.FileAccess.Write);
    //        newFile.Write(myData, 0, myData.Length);
    //        newFile.Close();
    //        //以上为上传原图
    //        try
    //        {
    //            //原图加载
    //            using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
    //            {
    //                //原图宽度和高度
    //                int width = sourceImage.Width;
    //                int height = sourceImage.Height;
    //                int smallWidth;
    //                int smallHeight;
    //                //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽 和 原图的高/缩略图的高)
    //                if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
    //                {
    //                    smallWidth = intThumbWidth;
    //                    smallHeight = intThumbWidth * height / width;
    //                }
    //                else
    //                {
    //                    smallWidth = intThumbHeight * width / height;
    //                    smallHeight = intThumbHeight;
    //                }
    //                ////判断缩略图在当前文件夹下是否同名称文件存在
    //                //file_append = 0;
    //                //sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
    //                sThumbFile = System.IO.Path.GetFileNameWithoutExtension(sFilename) + sThumbExtension + extendName;
    //                //while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
    //                //{
    //                //    file_append++;
    //                //    sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
    //                //        file_append.ToString() + extendName;
    //                //}
    //                //缩略图保存的绝对路径
    //                //string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
    //                string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
    //                //新建一个图板,以最小等比例压缩大小绘制原图
    //                using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
    //                {
    //                    //绘制中间图
    //                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
    //                    {
    //                        //高清,平滑
    //                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //                        g.Clear(Color.Black);
    //                        g.DrawImage(
    //                        sourceImage,
    //                        new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
    //                        new System.Drawing.Rectangle(0, 0, width, height),
    //                        System.Drawing.GraphicsUnit.Pixel
    //                        );
    //                    }
    //                    //新建一个图板,以缩略图大小绘制中间图
    //                    using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
    //                    {
    //                        //绘制缩略图
    //                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
    //                        {
    //                            //高清,平滑
    //                            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //                            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //                            g.Clear(Color.Black);
    //                            int lwidth = (smallWidth - intThumbWidth) / 2;
    //                            int bheight = (smallHeight - intThumbHeight) / 2;
    //                            g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
    //                            g.Dispose();
    //                            bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    //                        }
    //                    }
    //                }
    //            }
    //        }
    //        catch
    //        {
    //            //出错则删除
    //            System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
    //            return "图片格式不正确";
    //        }
    //        //返回缩略图名称
    //        return "";
    //    }
    //    return "没有选择图片";
    //}
    //#endregion

    protected void GetData()
    {
        if (Id != 0)
        {
            var info = ProductInfoBLL.GetProductInfo(Id);
            txt_Name.Text = info.Name;
            txt_desc.Text = info.Description;
        }
    }
    //绑定类别
    protected void GetProductType()
    {
        this.DropDownList1.DataSource = ProductTypeBLL.GetProductList("");
        this.DropDownList1.DataTextField = "Name";
        this.DropDownList1.DataValueField = "ID";
        this.DataBind();
    }
    //绑定门店
    protected void GetStores()
    {
        this.drp_store.DataSource = StoresInfoBLL.GetStoresList(new BarberShop.Model.StoresInfoSearch());
        this.drp_store.DataTextField = "StoreName";
        this.drp_store.DataValueField = "ID";
        this.drp_store.DataBind();
    }
    //添加
    protected void Button1_Click(object sender, EventArgs e)
    {

        //string ProductImgUrl = ConfigHelper.GetConfigString("ProductImg");
        string ProductImgUrl = ConfigHelper.GetConfigString("UpFileUrl") + "Shop_" + drp_store.SelectedValue.Trim() + "\\product\\";
        string bigimg = "";
        string smaillimg = "";
        string adname = txt_Name.Text.Trim();
        string r = "";
        if (File1.Value != "")
        {
            r = UpLoadImage(File1, ProductImgUrl, "_Thumb", 120, 120, out bigimg, out smaillimg);
        }
        if (r == "")
        {
            if (Id == 0)
            {
                ProductInfo info = new ProductInfo();
                info.Name = adname;
                info.Bigimgname = bigimg;
                info.Smallimgname = smaillimg;
                info.Type = int.Parse(DropDownList1.SelectedValue);
                info.Stores_ID = int.Parse(drp_store.SelectedValue);
                info.Description = txt_desc.Text.Trim();
                info.CreateDate = DateTime.Now;

                bool rr=ProductInfoBLL.AddProductInfo(info);
                if (rr)
                {
                    UpdateFiles fileinfo = new UpdateFiles();
                    //fileinfo.LinkPath = UpdateFileUrl + "keyner/shop/product/" + info.Bigimgname;
                    fileinfo.LinkPath = UpdateFileUrl + "Notice/DownloadFiles.ashx?Type=" + ((int)TableType.ProductInfoTable) + "&StoreId=" + drp_store.SelectedValue.Trim() + "&FileId=" + info.ID + "&fileName=" + info.Bigimgname;
                    fileinfo.Status = (int)FileStatus.Normal;
                    fileinfo.Stores_ID = info.Stores_ID;
                    fileinfo.TableID = (int)TableType.ProductInfoTable;
                    fileinfo.FileID = info.ID;
                    fileinfo.CreateDate = DateTime.Now;
                    fileinfo.FileName = info.Bigimgname;
                    fileinfo.FilePath = "keyner/shop/product/";
                    UpdateFilesBLL.Add(fileinfo);
                }
            }
            else
            {
                ProductInfo info = ProductInfoBLL.GetProductInfo(Id);
                info.Name = adname;
                info.Type = int.Parse(DropDownList1.SelectedValue);
                info.Stores_ID = int.Parse(drp_store.SelectedValue);
                info.Description = txt_desc.Text.Trim();
                if (File1.Value != "")
                {
                    info.Bigimgname = bigimg;
                    info.Smallimgname = smaillimg;
                    
                }
                ProductInfoBLL.UpdateProductInfo(info);
            }
            MessageBox.Show(this.Page, "保存成功！");

        }
        else
        {
            MessageBox.Show(this.Page, r);
        }
    }
   
}
